package tech.runchen.gz5055.oms.common.utils;

import tech.runchen.gz5055.oms.common.constant.HttpStatus;

import java.util.HashMap;

public class Result extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    public static final String CODE_TAG = "code";

    /**
     * 返回内容
     */
    public static final String MSG_TAG = "msg";

    /**
     * 数据对象
     */
    public static final String DATA_TAG = "data";

    /**
     * 状态码
     */
    public static final String SUCCESS_TAG = "success";

    public Result() {
    }

    public Result(int code, String msg, boolean success) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(SUCCESS_TAG, success);
    }

    public Result(int code, String msg, Object data, boolean success) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        super.put(SUCCESS_TAG, success);
        if (StringUtils.isNotNull(data)) {
            super.put(DATA_TAG, data);
        }
    }


    public static Result success() {
        return Result.success("操作成功");
    }

    public static Result success(Object data) {
        return Result.success("操作成功", data);
    }

    public static Result success(String msg) {
        return Result.success(msg, null);
    }

    public static Result success(String msg, Object data) {
        return new Result(HttpStatus.SUCCESS, msg, data, true);
    }

    public static Result error() {
        return Result.error("操作失败");
    }

    public static Result error(String msg) {
        return Result.error(msg, null);
    }

    public static Result error(String msg, Object data) {
        return new Result(HttpStatus.ERROR, msg, data, false);
    }

    public static Result error(int code, String msg) {
        return new Result(code, msg, false);
    }

    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
